import java.util.*;
import java.util.function.Predicate;

/**
 * describe:
 *
 * @author helloworldyu
 * @data 2017/11/30
 */
public class TestList {
	public static void main(String[] args) {
		List<Integer> list = new LinkedList<>();
		list.add(1);
		list.add(2);
		list.add(3);

		System.out.println("满足小于3:"+ list.stream().filter(o->(Integer)o<3).count());
//		replaceAll();

		sort();
//		System.out.println("满足小于1:"+ statistics(list,i->(Integer)i<1));
//		System.out.println("满足小于2:"+ statistics(list,i->(Integer)i<2));
//		System.out.println("满足小于3:"+ statistics(list,i->(Integer)i<3));

//		list.removeIf( o->o<3);
//		for ( Integer i:list){
//			System.out.println(i);
//		}

//		Iterator<Integer> iterator = list.iterator();
//		iterator.forEachRemaining( o-> System.out.println(o));

//		while( iterator.hasNext() ){
//			Integer next = iterator.next();
//			System.out.println(next);
//		}

		//		list.forEach( a-> System.out.println(a));
	}

	/**
	 * 统计 list 中满足条件的数据的个数
	 * @param list
	 * @param predicate
	 * @return
	 */
	private static int statistics(List<Integer> list,Predicate predicate){
		int total = 0;
		for ( Integer i: list ){
			if( predicate.test(i) ){
				total++;
			}
		}
		return total;
	}

	private static void sort(){
		List<String> books = new ArrayList();
		books.add("数据结构");
		books.add("并发编程2");
		books.add("netty 实战");

		System.out.println("方式一排序:");
		books.sort(Comparator.comparingInt(String::length));
		books.stream().forEach(o-> System.out.println(o));

		System.out.println("方式二排序:");
		books.sort((o1, o2) -> o2.length()-o1.length());
		books.stream().forEach(o-> System.out.println(o));
	}

	private static void replaceAll(){
		List books = new ArrayList();
		books.add((String)"数据结构");
		books.add((String)"并发编程2");
		books.add((String)"netty 实战");

		books.replaceAll( o -> ((String)o).length() );
		System.out.println("replaceAll 结果:");
		books.stream().forEach(o-> System.out.println(o));
	}
}